package com.xhwl.data.service.impl.personal;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.common.pojo.cent.sdata.personnel.StaffBusinessType;
import com.xhwl.common.query.cent.sdata.personal.StaffBusinessTypeQuery;
import com.xhwl.data.dao.personal.IStaffBusinessTypeDao;
import com.xhwl.data.service.personal.IStaffBusinessTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @description: 企业通讯录业务类型配置
 * @author: linruipeng
 * @create: 2022-05-20 10:30
 **/
@Service
public class StaffBusinessTypeServiceImpl extends ServiceImpl<IStaffBusinessTypeDao, StaffBusinessType> implements IStaffBusinessTypeService {

    @Autowired
    private IStaffBusinessTypeService staffBusinessTypeService;

    @Override
    public Page page(StaffBusinessTypeQuery staffBusinessTypeQuery, Page page) {
        QueryWrapper<StaffBusinessType> businessTypeQueryWrapper = new QueryWrapper<>();
        if (null != staffBusinessTypeQuery.getName()) {
            businessTypeQueryWrapper.like("name", staffBusinessTypeQuery.getName());
        }
        return staffBusinessTypeService.page(page, businessTypeQueryWrapper);
    }


    @Override
    public List<StaffBusinessType> list(StaffBusinessTypeQuery staffBusinessTypeQuery) {
        QueryWrapper<StaffBusinessType> businessTypeQueryWrapper = new QueryWrapper<>();
        if (null != staffBusinessTypeQuery.getName()) {
            businessTypeQueryWrapper.like("name", staffBusinessTypeQuery.getName());
        }
        if (!CollectionUtils.isEmpty(staffBusinessTypeQuery.getIdList())) {
            businessTypeQueryWrapper.in("id", staffBusinessTypeQuery.getIdList());
        }
        List<StaffBusinessType> list = staffBusinessTypeService.list(businessTypeQueryWrapper);
        return list;
    }


    @Override
    public ResultJson create(StaffBusinessType staffBusinessType) {
        QueryWrapper<StaffBusinessType> staffBusinessTypeQueryWrapper = new QueryWrapper<>();
        staffBusinessTypeQueryWrapper.eq("name", staffBusinessType.getName());
        StaffBusinessType one = staffBusinessTypeService.getOne(staffBusinessTypeQueryWrapper);
        if (null != one) {
            return ResultJson.fail("业务类型名称已存在");
        }
        staffBusinessTypeService.save(staffBusinessType);
        return ResultJson.success();
    }


    @Override
    public ResultJson update(StaffBusinessType staffBusinessType) {
        StaffBusinessType one = staffBusinessTypeService.getById(staffBusinessType.getId());
        if (null == one) {
            return ResultJson.fail("数据不存在");
        }
        QueryWrapper<StaffBusinessType> staffBusinessTypeQueryWrapper = new QueryWrapper<>();
        staffBusinessTypeQueryWrapper.eq("name", staffBusinessType.getName());
        StaffBusinessType oldOne = staffBusinessTypeService.getOne(staffBusinessTypeQueryWrapper);
        if (null != oldOne && oldOne.getId() != staffBusinessType.getId()) {
            return ResultJson.fail("业务类型名称已存在");
        }
        return ResultJson.success(this.updateById(staffBusinessType));
    }


    @Override
    public ResultJson getOne(Integer id) {
        StaffBusinessType one = staffBusinessTypeService.getById(id);
        if (null == one) {
            return ResultJson.fail("数据不存在");
        }
        return ResultJson.success(one);
    }

}
